﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
using SymulatorEthernetWPF.Shared;
using SymulatorEthernetWPF.UserInterface.UIElements;
using SymulatorEthernetWPF.UserInterface;
using SymulatorEthernetWPF.SimObjects;
using SymulatorEthernetWPF.SimObjects.Devices;
using System.Collections.Generic;

namespace SymulatorEthernetWPF.UserInterface
{
    /// <summary>
    /// Interaction logic for DeviceSettings.xaml
    /// </summary>
    public partial class HostSettings : Window
    {
        private DeviceControl _deviceControl {get; set;}
        private List<TaskQueueItem> _toRemove = new List<TaskQueueItem>();

        public HostSettings(DeviceControl deviceControl)
        {
            InitializeComponent();
            _deviceControl = deviceControl;
            deviceName.Text = _deviceControl.GetDeviceLabel();
            deviceIcon.Source = deviceControl.icon.Source;
            interface1Name.Text = _deviceControl.interfaces[0].Name;
            interface2Name.Text = _deviceControl.interfaces[1].Name;
            interface3Name.Text = _deviceControl.interfaces[2].Name;
            interface4Name.Text = _deviceControl.interfaces[3].Name;
            interface5Name.Text = _deviceControl.interfaces[4].Name;
            interface6Name.Text = _deviceControl.interfaces[5].Name;
            interface7Name.Text = _deviceControl.interfaces[6].Name;
            interface8Name.Text = _deviceControl.interfaces[7].Name;
            if (_deviceControl.GetType() == typeof(HubControl))
            {
                interface1Mac.IsEnabled = false;
                interface2Mac.IsEnabled = false;
                interface3Mac.IsEnabled = false;
                interface4Mac.IsEnabled = false;
                interface5Mac.IsEnabled = false;
                interface6Mac.IsEnabled = false;
                interface7Mac.IsEnabled = false;
                interface8Mac.IsEnabled = false;
            }
            else
            {
                interface1Mac.Text = _deviceControl.interfaces[0].Adress.Adress;
                interface2Mac.Text = _deviceControl.interfaces[1].Adress.Adress;
                interface3Mac.Text = _deviceControl.interfaces[2].Adress.Adress;
                interface4Mac.Text = _deviceControl.interfaces[3].Adress.Adress;
                interface5Mac.Text = _deviceControl.interfaces[4].Adress.Adress;
                interface6Mac.Text = _deviceControl.interfaces[5].Adress.Adress;
                interface7Mac.Text = _deviceControl.interfaces[6].Adress.Adress;
                interface8Mac.Text = _deviceControl.interfaces[7].Adress.Adress;
            }

            isInterface1Enabled.IsChecked = _deviceControl.interfaces[0].IsEnabled;
            isInterface2Enabled.IsChecked = _deviceControl.interfaces[1].IsEnabled;
            isInterface3Enabled.IsChecked = _deviceControl.interfaces[2].IsEnabled;
            isInterface4Enabled.IsChecked = _deviceControl.interfaces[3].IsEnabled;
            isInterface5Enabled.IsChecked = _deviceControl.interfaces[4].IsEnabled;
            isInterface6Enabled.IsChecked = _deviceControl.interfaces[5].IsEnabled;
            isInterface7Enabled.IsChecked = _deviceControl.interfaces[6].IsEnabled;
            isInterface8Enabled.IsChecked = _deviceControl.interfaces[7].IsEnabled;
            foreach (TaskQueueItem ti in Simulation.taskQueue)
            {

                if (ti.sourceDeviceGUID == _deviceControl.GUID)
                {
                    TimetableItem timetableItem = new TimetableItem(_deviceControl, ti);
                    timetableItem.deleteEvent += timetableItem_deleteEvent;
                    timetableStack.Children.Add(timetableItem);
                }

            }

        }

        private void CancelButtonClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void OkButtonClick(object sender, RoutedEventArgs e)
        {
            Simulation.UndoStack.Push(new HistoryElement());
            Simulation.RedoStack.Clear();


            foreach (TimetableItem it in timetableStack.Children)
            {
                it.save();
            }
            foreach (TaskQueueItem it in _toRemove)
            {
                Simulation.taskQueue.Remove(it);
            }
            
            _deviceControl.SetDeviceLabel(deviceName.Text);
            _deviceControl.interfaces[0].Name = interface1Name.Text;
            _deviceControl.interfaces[1].Name = interface2Name.Text;
            _deviceControl.interfaces[2].Name = interface3Name.Text;
            _deviceControl.interfaces[3].Name = interface4Name.Text;
            _deviceControl.interfaces[4].Name = interface5Name.Text;
            _deviceControl.interfaces[5].Name = interface6Name.Text;
            _deviceControl.interfaces[6].Name = interface7Name.Text;
            _deviceControl.interfaces[7].Name = interface8Name.Text;

            _deviceControl.interfaces[0].IsEnabled = (bool)isInterface1Enabled.IsChecked;
            _deviceControl.interfaces[1].IsEnabled = (bool)isInterface2Enabled.IsChecked;
            _deviceControl.interfaces[2].IsEnabled = (bool)isInterface3Enabled.IsChecked;
            _deviceControl.interfaces[3].IsEnabled = (bool)isInterface4Enabled.IsChecked;
            _deviceControl.interfaces[4].IsEnabled = (bool)isInterface5Enabled.IsChecked;
            _deviceControl.interfaces[5].IsEnabled = (bool)isInterface6Enabled.IsChecked;
            _deviceControl.interfaces[6].IsEnabled = (bool)isInterface7Enabled.IsChecked;
            _deviceControl.interfaces[7].IsEnabled = (bool)isInterface8Enabled.IsChecked;

            _deviceControl.interfaces[0].Adress = new MacAdress(this.interface1Mac.Text);
            _deviceControl.interfaces[1].Adress = new MacAdress(this.interface2Mac.Text);
            _deviceControl.interfaces[2].Adress = new MacAdress(this.interface3Mac.Text);
            _deviceControl.interfaces[3].Adress = new MacAdress(this.interface4Mac.Text);
            _deviceControl.interfaces[4].Adress = new MacAdress(this.interface5Mac.Text);
            _deviceControl.interfaces[5].Adress = new MacAdress(this.interface6Mac.Text);
            _deviceControl.interfaces[6].Adress = new MacAdress(this.interface7Mac.Text);
            _deviceControl.interfaces[7].Adress = new MacAdress(this.interface8Mac.Text);

            _deviceControl.icon.Source = deviceIcon.Source;
            using (var stream = new MemoryStream())
            {
                var encoder = new PngBitmapEncoder {Interlace = PngInterlaceOption.On};
                encoder.Frames.Add(BitmapFrame.Create((BitmapSource) _deviceControl.icon.Source));
                encoder.Save(stream);
                _deviceControl.RawImageData = stream.ToArray();
            }

            InfoBar.deviceControl = _deviceControl;
            Close();
        }

        private void AddTaskButtonClick(object sender, RoutedEventArgs e)
        {
            try
            {

                TimetableItem timetableItem;
                if (!(bool)DoOnce.IsChecked)
                {
                    timetableItem = new TimetableItem(_deviceControl, new MacAdress(sourceSelectionTextbox.Text), new MacAdress(destinationSelectionTextbox.Text), timeInput.Number, repeatInput.Number);
                }
                else
                {
                    timetableItem = new TimetableItem(_deviceControl, new MacAdress(sourceSelectionTextbox.Text), new MacAdress(destinationSelectionTextbox.Text), timeInput.Number, -1);
                }

                timetableStack.Children.Add(timetableItem);
                timetableItem.deleteEvent +=timetableItem_deleteEvent;
            }
            catch
            {
                
            }
        }

        void timetableItem_deleteEvent(TimetableItem timetableItem, TaskQueueItem taskQueueItem)
        {
            timetableStack.Children.Remove(timetableItem);
            if (taskQueueItem != null)
                _toRemove.Add(taskQueueItem);
        }

        private void IconButtonClick(object sender, RoutedEventArgs e)
        {
            var dlg = new OpenFileDialog {DefaultExt = ".jpg", Filter = "Obrazy (.jpg .png .bmp)|*.jpg;*.png;*bmp"};

            bool? result = dlg.ShowDialog();
            if (result == true)
            {
                string filename = dlg.FileName;
                var imgConv = new ImageSourceConverter();
                var imageSource = (ImageSource) imgConv.ConvertFromString(filename);
                deviceIcon.Source = StaticFunctions.ResizeImage(imageSource);
            }
        }

        private void selectSourceButton_Click(object sender, RoutedEventArgs e)
        {
            AdressSelection adressSelection = new AdressSelection(_deviceControl, false);
            adressSelection.AdressChanged += adressSelection_SourceAdressChanged;
            adressSelection.ShowDialog();
        }

        void adressSelection_SourceAdressChanged(MacAdress adress)
        {
            sourceSelectionTextbox.Text = adress.ToString();
        }

        private void selectDestinationButton_Click(object sender, RoutedEventArgs e)
        {
            AdressSelection adressSelection = new AdressSelection(_deviceControl, true);
            adressSelection.AdressChanged += adressSelection_DestinationAdressChanged;
            adressSelection.ShowDialog();
        }

        private void adressSelection_DestinationAdressChanged(MacAdress adress)
        {
            destinationSelectionTextbox.Text = adress.ToString();
        }

        

    }
}